﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ZeroTouchUI
{
    /// <summary>
    /// Interaction logic for Edit.xaml
    /// </summary>
    public partial class Edit : Window
    {
        Profile profile;
        MainWindow mainWindow;
        GestureCanvas canvas;
        public bool delete = false;

        public Edit(MainWindow mW)
        {
            InitializeComponent();
            canvas = new GestureCanvas();
            mainWindow = mW;
            gestureCanvas.Children.Add(canvas);
            EditSelected.IsEnabled = false;
            RemoveSelected.IsEnabled = false;
        }

        public Edit(Profile p, MainWindow mW)
        {
            InitializeComponent();
            profile = p;
            mainWindow = mW;
            EditSelected.IsEnabled = false;
            RemoveSelected.IsEnabled = false;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void AddGesture_Click(object sender, RoutedEventArgs e)
        {
            AddGesture ag = new AddGesture(profile, mainWindow, this);
            ag.ShowDialog();
        }

        

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Update();            
        }

        public void Update()
        {
            List<string> gestures = new List<string>();
            foreach (KeyValuePair<Gesture, Action> pair in profile.getDictionary())
                gestures.Add(pair.Key.Name);

            gestureView.BeginInit();
            gestureView.ItemsSource = gestures.AsEnumerable<string>();
            gestureView.EndInit();

            if (gestureView.SelectedItem != null)
            {
                Gesture active = new Gesture();
                foreach (Gesture g in mainWindow.profile.getDictionary().Keys)
                     if (g.Name == gestureView.SelectedItem.ToString())
                         active = g;
                actionText.Text = profile.getDictionary()[active].Name;
            }

            if (profile != null && profile.Name != "")
            {
                profile.SaveProfile(profile.Name);
            }

        }

        private void gestureView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                gestureCanvas.Children.Remove(canvas);
                Gesture shownGesture = new Gesture();
                foreach (Gesture gesture in profile.getDictionary().Keys)
                {
                    if (gesture.Name == e.AddedItems[0].ToString())
                    {
                        shownGesture = gesture;
                        break;
                    }
                }
                canvas = new GestureCanvas { gesture = shownGesture };
                gestureCanvas.Children.Add(canvas);
                actionText.Text = profile.getDictionary()[shownGesture].Name;
                EditSelected.IsEnabled = true;
                RemoveSelected.IsEnabled = true;
            }
            else
            {
                EditSelected.IsEnabled = false;
                RemoveSelected.IsEnabled = false;
            }
        }

        private void EditSelected_Click(object sender, RoutedEventArgs e)
        {
            Gesture edit = new Gesture();
            foreach (Gesture g in mainWindow.profile.getDictionary().Keys)
                if (g.Name == gestureView.SelectedItem.ToString())
                    edit = g;
            AddGesture add = new AddGesture(profile, mainWindow, this, edit, profile.getDictionary()[edit]);
            add.ShowDialog();
        }

        private void RemoveSelected_Click(object sender, RoutedEventArgs e)
        {
            ConfirmRemove cr = new ConfirmRemove(this);
            cr.ShowDialog();
            if (delete && gestureView.SelectedItem != null)
            {
                Gesture remove = new Gesture();
                foreach (Gesture g in mainWindow.profile.getDictionary().Keys)
                    if (g.Name == gestureView.SelectedItem.ToString())
                        remove = g;
                mainWindow.profile.getDictionary().Remove(remove);
                Update();
                gestureCanvas.Children.Remove(canvas);
            }
        }
    }
}
